#include "cmsis_os2.h"
#include "lvgl.h"


extern osMutexId_t LvglMutexHandle;

lv_obj_t* label_input = NULL;

static void btnm_event_cb(lv_event_t* e)
{
    uint32_t id;
    lv_event_code_t code = lv_event_get_code(e);                                    /* 获取事件类型 */
    lv_obj_t* target = lv_event_get_target(e);                                      /* 获取触发源 */
    if (code == LV_EVENT_VALUE_CHANGED)
    {
        id = lv_btnmatrix_get_selected_btn(target);                                 /* 获取按钮索引 */
        lv_label_set_text(label_input, lv_btnmatrix_get_btn_text(target, id));      /* 更新输入框标签文本 */
    }
}

void StartLvglTask(void *argument)
{
    osMutexAcquire(LvglMutexHandle, osWaitForever);

    int32_t  with = lv_obj_get_width(lv_scr_act());
    int32_t  height = lv_obj_get_height(lv_scr_act());

    lv_obj_t* label_user = lv_label_create(lv_scr_act());                           /* 创建 USER 标签 */
    lv_label_set_text(label_user, "USER");                                          /* 设置文本 */
    lv_obj_set_style_text_font(label_user, &lv_font_montserrat_24, LV_PART_MAIN);   /* 设置字体 */
    lv_obj_set_style_text_align(label_user, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);    /* 设置文本居中 */ 
    lv_obj_align(label_user, LV_ALIGN_CENTER, -with / 4, -height / 7);           	/* 设置位置 */

    lv_obj_t* obj_input = lv_obj_create(lv_scr_act());                              /* 创建基础对象-输入背景框 */
    lv_obj_set_size(obj_input, with/4, height/12);                                  /* 设置大小 */
    lv_obj_align_to(obj_input, label_user, LV_ALIGN_OUT_BOTTOM_MID, 0, height/20);  /* 设置位置 */ 
    lv_obj_set_style_bg_color(obj_input, lv_color_hex(0xcccccc),0);                 /* 设置背景颜色 */
    lv_obj_set_style_bg_opa(obj_input, 150, 0);                                     /* 设置透明度 */
    lv_obj_set_style_border_width(obj_input, 0 , 0);                                /* 去除边框 */
    lv_obj_set_style_radius(obj_input, 20, 0);                                      /* 设置圆角 */
    lv_obj_remove_style(obj_input, NULL, LV_PART_SCROLLBAR);                        /* 移除滚动条 */

    label_input = lv_label_create(lv_scr_act());                                    /* 创建输入框文本标签 */
    lv_label_set_text(label_input, "");                                             /* 设置文本 */
    lv_obj_set_style_text_font(label_input, &lv_font_montserrat_24, LV_PART_MAIN);  /* 设置字体 */
    lv_obj_set_style_text_align(label_input, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);   /* 设置文本居中 */
    lv_obj_align_to(label_input, obj_input, LV_ALIGN_CENTER, 0, 0);                 /* 设置位置 */

    lv_obj_t *line = lv_line_create(lv_scr_act());                                  /* 创建线条 */
    static lv_point_precise_t points[] = {{0,0},{0,200}};                           /* 线条坐标点数组 */
    lv_line_set_points(line, points, 2);                                            /* 设置线条坐标点 */
    lv_obj_align(line, LV_ALIGN_CENTER, 0, 0);                                      /* 设置位置 */
    lv_obj_set_style_line_color(line, lv_color_hex(0xcdcdcd),0);                    /* 设置线条颜色 */

    lv_obj_t* btnm = lv_btnmatrix_create(lv_scr_act());                             /* 创建按钮矩阵 */
    lv_obj_set_size(btnm, with* 2/5, with* 2/5);                                    /* 设置大小 */
    /* 按钮矩阵数组 */
    static const char* num_map[] = {"1", "2", "3", "\n", \
                                    "4", "5", "6", "\n", \
                                    "7", "8", "9", "\n", \
                                    "#", "0", "%", "" }; 
    lv_btnmatrix_set_map(btnm, num_map);                                            /* 设置按钮 */ 
    lv_obj_align(btnm, LV_ALIGN_RIGHT_MID, -with/16, 0);                            /* 设置位置 */ 
    lv_obj_set_style_text_font(btnm, &lv_font_montserrat_24, LV_PART_ITEMS);        /* 设置字体 */ 
    lv_obj_set_style_border_width(btnm, 0, LV_PART_MAIN);                           /* 去除主体边框 */
    lv_obj_set_style_bg_opa(btnm, 0, LV_PART_MAIN);                                 /* 设置主体背景透明度 */
    lv_obj_set_style_bg_opa(btnm, 0, LV_PART_ITEMS);                                /* 设置按钮背景透明度 */
    lv_obj_set_style_shadow_width(btnm, 0, LV_PART_ITEMS);                          /* 去除按钮阴影 */

    lv_obj_add_event_cb(btnm, btnm_event_cb, LV_EVENT_VALUE_CHANGED, NULL);         /* 设置按钮矩阵回调 */

    osMutexRelease(LvglMutexHandle);

    for(;;)
    {
        osMutexAcquire(LvglMutexHandle, osWaitForever);
        lv_timer_handler();
        osMutexRelease(LvglMutexHandle);
        osDelay(5);
    }
}
